package com.czhappy.huans.controller;

import com.czhappy.huans.entity.Area;
import com.czhappy.huans.exception.Result;
import com.czhappy.huans.exception.ResultUtil;
import com.czhappy.huans.service.AreaService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private AreaService areaService;

    /**
     * 获取所有的区域信息
     *
     * @return
     */
    @RequestMapping(value = "/listArea", method = RequestMethod.GET)
    private Result listArea() {
        List<Area> list = new ArrayList<Area>();
        // 获取区域列表
        list = areaService.getAreaList();
        return ResultUtil.success(list);
    }

    /**
     * 通过区域Id获取区域信息
     *
     * @return
     */
    @RequestMapping(value = "/getAreaById", method = RequestMethod.GET)
    private Result getAreaById(Integer areaId) {
        // 获取区域信息
        Area area = areaService.getAreaById(areaId);
        return ResultUtil.success(area);
    }

    /**
     * 添加区域信息
     *
     * @return
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    @RequestMapping(value = "/addArea", method = RequestMethod.POST)
    private Result addArea(@RequestBody Area area)
            throws JsonParseException, JsonMappingException, IOException {
        // 添加区域信息
        boolean isSuccess = areaService.addArea(area);
        if (isSuccess) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error();
        }
    }

    /**
     * 修改区域信息，主要修改名字
     *
     * @return
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    @RequestMapping(value = "/modifyArea", method = RequestMethod.POST)
    private Result modifyArea(@RequestBody Area area)
            throws JsonParseException, JsonMappingException, IOException {
        // 修改区域信息
        boolean isSuccess = areaService.modifyArea(area);
        if (isSuccess) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error();
        }
    }

    @RequestMapping(value = "/removeArea", method = RequestMethod.GET)
    private Result removeArea(Integer areaId) {
        // 删除区域信息
        boolean isSuccess = areaService.deleteArea(areaId);
        if (isSuccess) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error();
        }
    }

}
